<?php
/**
 * 媒资管理
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\sysusemanage;
use app\api\controller\common\ApiBase;
use app\api\model\MediaFiles;
use app\api\service\Uploader;
use think\Db;
use app\api\controller\common\CommonBasic;
class Main extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companymgr');
    }


    public function getInfo(){

        $combasic =new CommonBasic();
        $parent=array();
//        filetypearr 【{title:'图片',zb:'10','dx':'3M'},{title:'视频',zb:'10','dx':'3M'}】

        $list = (new MediaFiles())->where(['is_deleted' => '0', 'status' => '1'] )->field('count(*) as count, sum(file_size) as total,type')->group('type')->select();

        $splist = array(
            array(
                'title'   => '图片',
                'filenum' => 10,
                'space'   => '0M',
                'filesize'=> '0'
            ),
            array(
                'title'   => '视频',
                'filenum' => 100,
                'space'   => '0M',
                'filesize'=> '0'
            ),
            array(
                'title'   => '其他',
                'filenum' => "",
                'space'   => '0M',
                'filesize'=> '0'
            ),
            array(
                'title'   => '空闲',
                'filenum' => "",
                'space'   => '0M',
                'filesize'=> '0'
            )
        );

        $sys_spacetotalnum=(disk_total_space('.'));
        $sys_spacefreenum=(disk_free_space('.'));

        foreach ($list as $item)  {
            $splist[$item['type']]['filenum'] = $item['count'];
            $splist[$item['type']]['space'] = (new Uploader())->getFilesize($item['total']);
            $splist[$item['type']]['filesize'] = $item['total'];
        }
        $splist[2]['filesize'] = $sys_spacetotalnum - $sys_spacefreenum - $splist[0]['filesize'] - $splist[1]['filesize'];
        $splist[2]['space'] = (new Uploader())->getFilesize($splist[2]['filesize']);
        $splist[3]['filesize'] = $sys_spacefreenum;
        $splist[3]['space'] = (new Uploader())->getFilesize($sys_spacefreenum);

        $parent['filetypearr']=$splist;
        $parent['picusepre']=10;
        $parent['picusenum']=24;
        $parent['picusespace']='1.0t';



        $parent['videousepre']=40;
        $parent['videousenum']=30;
        $parent['videousespace']='10t';

        $parent['nousepre']=45;
        $parent['nousenum']=30;
        $parent['nousespace']='10t';

        $parent['otherpre']=5;
        $parent['othernum']=30;
        $parent['otherspace']='10t';



        $sysusepre=round((($sys_spacetotalnum - $sys_spacefreenum)/$sys_spacetotalnum)*100,0)."%";
        $parent['sysusepre']=$sysusepre;
//        $parent['sys_spacetotal']= $combasic->get_disk_total(disk_total_space('.'));
//        $parent['sys_spacefree']=$combasic->get_disk_total(disk_free_space('.'));// 当前磁盘的剩余空间
        $parent['sys_spacetotal']= (new Uploader())->getFilesize(disk_total_space('.'));
        $parent['sys_spacefree']= (new Uploader())->getFilesize(disk_free_space('.'));// 当前磁盘的剩余空间
        return json(['code'=>1,'msg'=>'success','data'=>$parent]);
    }








    public function getList(){
        $parent=db('jq_base_cate')->where(['is_delete'=>0,'parent_id'=>0])->order('sort desc')->select();
        $list=db('jq_base_cate')->where(['is_delete'=>0,'parent_id'=>array('gt',0)])->select();
        foreach ($parent as &$v){
            $v['children']=[];
            foreach ($list as &$vv){
                if($vv['parent_id']==$v['id']){
                    $v['children'][]=$vv;
                }
            }
        }
        return json(['code'=>1,'msg'=>'success','data'=>$parent]);
    }



    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $data['create_time']=time();
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['iswarn']=$this->request->param('iswarn');
//        $data['page_type']=$this->request->param('page_type');
        $parent_id=$this->request->param('parent_id');
        $data['parent_id']=$parent_id?$parent_id:0;
        $data['level']=$parent_id?1:0;
        $res=db('jq_base_cate')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('id');
        $r = db('jq_base_cate')->where(['id'=>$id,'is_delete'=>0])->find();
        if(empty($r)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $r = db('jq_base_cate')->where(['id'=>$id])->update(['is_delete'=>1]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }



    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['iswarn']=$this->request->param('iswarn');
        $parent_id=$this->request->param('parent_id');
//        $data['page_type']=$this->request->param('page_type');
        $data['parent_id']=$parent_id?$parent_id:0;
        $data['level']=$parent_id?1:0;
//        var_dump( $data);die;
        $where['id'] = $id;
        $r=db('jq_base_cate')->where($where)->update($data);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }







}